package com.icen.utils.baidu.loc;

import android.content.Context;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;

/**
 * Baidu Location Utils
 * 
 * @author IcenHan 2015-01-19
 * 
 */
public class BaiduLocUtils {
	private Context mContext;
	private boolean isShowLog = false;

	// 单例模式：私有化构造函数
	private BaiduLocUtils(Context context) {
		this.mContext = context;
	}

	private static BaiduLocUtils mBaiduLocUtils = null;

	public static BaiduLocUtils getInstance(Context context) {
		if (mBaiduLocUtils == null) {
			mBaiduLocUtils = new BaiduLocUtils(context);
		}
		return mBaiduLocUtils;
	}

	private static LocationClient mBaiduLocClient = null;

	private void initBaiduLoc() {
		if (mBaiduLocClient == null) {
			mBaiduLocClient = new LocationClient(mContext);
			mBaiduLocClient.registerLocationListener(new BaiduLocLinstener());
		}
	}

	// 设置百度定位参数值
	private void setBadiLocOption() {
		if (mBaiduLocClient != null) {
			LocationClientOption option = new LocationClientOption();
			option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式
			option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
			option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
			option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
			option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向
			mBaiduLocClient.setLocOption(option);
		}
	}

	/**
	 * 百度定位启动入口;生命周期,建议跟随ApplicationContext启动
	 */
	public void onStartBaiduLoc() {
		// 初始化百度定位类
		this.initBaiduLoc();
		// 设置百度定位参数
		this.setBadiLocOption();
		// 启动百度定位
		this.startLoc();
	}

	private void startLoc() {
		if (mBaiduLocClient != null && !mBaiduLocClient.isStarted()) {
			mBaiduLocClient.start();
		}
	}

	/**
	 * 百度定位关闭退出; 生命周期,对应Activity生命周期onStop
	 */
	public void onStopBaiduLoc() {
		if (mBaiduLocClient != null && mBaiduLocClient.isStarted()) {
			mBaiduLocClient.stop();
		}
	}

	/**
	 * 百度定位回调接口
	 */
	public interface BaiduLocCallBack {
		void onBaiduLocInfo(BaiduLocInfo locInfo);
	}

	private BaiduLocCallBack mBaiduLocCallBack = null;

	/**
	 * 添加百度定位回调函数
	 * 
	 * @param callBack
	 */
	public void setBaiduLocCallBack(BaiduLocCallBack callBack) {
		this.mBaiduLocCallBack = callBack;
	}

	// 定位监听内部类
	class BaiduLocLinstener implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation loc) {
			if (loc == null) {
				return;
			}

			BaiduLocInfo baiduLocInfo = BaiduLocInfo.getInstance();
			baiduLocInfo.setLocTime(loc.getTime());
			baiduLocInfo.setLocType(loc.getLocType());
			baiduLocInfo.setLatitude(loc.getLatitude());
			baiduLocInfo.setLongitude(loc.getLongitude());
			baiduLocInfo.setRadius(loc.getRadius());
			baiduLocInfo.setAddressStr(loc.getAddrStr());
			baiduLocInfo.setProvince(loc.getProvince());
			baiduLocInfo.setCity(loc.getCity());
			baiduLocInfo.setDistrict(loc.getDistrict());
			baiduLocInfo.setDirection(loc.getDirection());

			if (mBaiduLocCallBack != null) {
				mBaiduLocCallBack.onBaiduLocInfo(baiduLocInfo);
			}

			if (isShowLog) {
				Log.i("[BaiduLocUtils|BaiduLocInfo]", baiduLocInfo.toString());
			}
		}
	}

	/**
	 * 是否打印日志
	 * 
	 * @param isShowLog
	 */
	public void setShowLog(boolean isShowLog) {
		this.isShowLog = isShowLog;
	}

}
